package com.test.cordova.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.test.cordova.R;


/**
 * 自定义Dialog
 */

public class CustomDialog extends Dialog {

    private static OnKeyListener keylistener = new OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                return true;
            } else {
                return false;
            }
        }
    };

    private View inflate;
    private int negativevIsvisiable = View.VISIBLE;
    private int positiveIsvisiable = View.VISIBLE;

    private CustomDialog(@NonNull Context context,View inflate) {
        super(context, android.R.style.Theme_Holo_Light_Dialog_NoActionBar);
        this.inflate = inflate;
        requestWindowFeature(Window.FEATURE_NO_TITLE);//设置全屏
    }

    public void setContent(String content){
        if (!TextUtils.isEmpty(content)) {
            ((TextView) inflate.findViewById(R.id.custom_tip_content)).setText(content);
        }
    }

    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            ((TextView) inflate.findViewById(R.id.custom_tip_title)).setText(title);
        }
    }

    public void setPositiveText(String positive) {
        if (!TextUtils.isEmpty(positive)) {
            ((TextView) inflate.findViewById(R.id.custom_tip_confirm)).setText(positive);
        }
    }

    public void setNegativeText(String negative) {
        if (!TextUtils.isEmpty(negative)) {
            ((TextView) inflate.findViewById(R.id.custom_tip_cancel)).setText(negative);
        }
    }

    public void setNegativeVisibility(int visibility) {
        negativevIsvisiable = visibility;
        inflate.findViewById(R.id.custom_tip_cancel).setVisibility(negativevIsvisiable);
        setLineStatu();
    }

    public void setPositiveVisibility(int visibility) {
        positiveIsvisiable = visibility;
        inflate.findViewById(R.id.custom_tip_confirm).setVisibility(positiveIsvisiable);
        setLineStatu();
    }

    private void setLineStatu(){
        if (negativevIsvisiable == View.GONE && positiveIsvisiable == View.GONE) {
            inflate.findViewById(R.id.custom_dialog_line).setVisibility(View.GONE);
            inflate.findViewById(R.id.custom_dialog_bottom_ll).setVisibility(View.GONE);
        }
    }

    public static class DialogBuilder implements View.OnClickListener {

        public DialogBuilder(Context context) {
            this.context = context;
        }

        private Context context;
        private OnNegativeClickListener onNegativeClickListener;
        private OnPositiveClickListener onPositiveClickListener;
        private CustomDialog dialog;
        private String content;
        private String title;
        private String positive;
        private String negative;
        private int negativevIsvisiable = View.VISIBLE;
        private int positiveIsvisiable = View.VISIBLE;

        public DialogBuilder setContent(String content) {
            this.content = content;
            return this;
        }

        public DialogBuilder setTitle(String title) {
            this.title = title;
            return this;
        }

        public DialogBuilder setPositive(String positive, OnPositiveClickListener onPositiveClickListener) {
            this.onPositiveClickListener = onPositiveClickListener;
            this.positive = positive;
            return this;
        }

        public DialogBuilder setNegative(String negative, OnNegativeClickListener onNegativeClickListener) {
            this.onNegativeClickListener = onNegativeClickListener;
            this.negative = negative;
            return this;
        }

        public DialogBuilder setNegativeVisibility(int visibility) {
            this.negativevIsvisiable = visibility;
            return this;
        }

        public DialogBuilder setPositiveVisibility(int visibility) {
            this.positiveIsvisiable = visibility;
            return this;
        }

        public CustomDialog createDialog() {

            View inflate = LayoutInflater.from(context).inflate(R.layout.custom_tip_layout, null, false);
            dialog = new CustomDialog(context,inflate);
            inflate.findViewById(R.id.custom_tip_cancel).setOnClickListener(this);
            inflate.findViewById(R.id.custom_tip_confirm).setOnClickListener(this);
            inflate.findViewById(R.id.custom_tip_cancel).setVisibility(negativevIsvisiable);
            inflate.findViewById(R.id.custom_tip_confirm).setVisibility(positiveIsvisiable);
            if (negativevIsvisiable == View.GONE && positiveIsvisiable == View.GONE) {
                inflate.findViewById(R.id.custom_dialog_line).setVisibility(View.GONE);
                inflate.findViewById(R.id.custom_dialog_bottom_ll).setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(negative)) {
                ((TextView) inflate.findViewById(R.id.custom_tip_cancel)).setText(negative);
            }
            if (!TextUtils.isEmpty(positive)) {
                ((TextView) inflate.findViewById(R.id.custom_tip_confirm)).setText(positive);
            }

            if (!TextUtils.isEmpty(title)) {
                ((TextView) inflate.findViewById(R.id.custom_tip_title)).setText(title);
            }
            if (!TextUtils.isEmpty(content)) {
                ((TextView) inflate.findViewById(R.id.custom_tip_content)).setText(content);
            }

            dialog.setContentView(inflate);
            //点击对话框外面,对话框不消失
            dialog.setCanceledOnTouchOutside(false);
            dialog.setOnKeyListener(keylistener);
            dialog.setCancelable(false);

            return dialog;
        }

        @Override
        public void onClick(View v) {
            dialog.dismiss();
            switch (v.getId()) {
                case R.id.custom_tip_cancel:
                    if (onNegativeClickListener != null) {
                        onNegativeClickListener.onCallback();
                    }
                    break;
                case R.id.custom_tip_confirm:
                    if (onPositiveClickListener != null) {
                        onPositiveClickListener.onCallback();
                    }
                    break;
            }
        }
    }


    public interface OnNegativeClickListener {
        void onCallback();
    }

    public interface OnPositiveClickListener {
        void onCallback();
    }

}
